package com.xl.rentkeeper.business;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.SyncHttpClient;
import com.xl.rentkeeper.App;
import com.xl.rentkeeper.log.QLog;
import com.xl.rentkeeper.log.ZipResultCallback;
import com.xl.rentkeeper.mgr.UiObserverManager;

import org.apache.http.Header;

import java.io.File;

/**
 * Created by Administrator on 2015/9/2.
 */
public class LogUploadLogic implements ZipResultCallback {
    public static final String CMD_UPLOAD_LOG = "CMD_UPLOAD_LOG";

    public static LogUploadLogic getInstance() {
        return (LogUploadLogic) SingletonRegistry.getInstance(LogUploadLogic.class);
    }

    @Override
    public void zipOk(String zipPath) {
        uploadLog(zipPath);
    }

    @Override
    public void zipFail(String errInfo) {
        UiObserverManager.getInstance().dispatchEvent(CMD_UPLOAD_LOG, false, errInfo, null);
    }

    private LogUploadLogic() {

    }

    public void uploadLog(final long startTime, final long endTime) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                QLog.reportLog(startTime, endTime, LogUploadLogic.this);
            }
        }, "uploadlog").start();
    }

    private void uploadLog(String filePath) {
        String url = "http://192.168.1.9:8080/logServer/upload.do";
        File file = new File(filePath);
        QLog.d(this,"文件名字: "+file.getName());

        SyncHttpClient client = new SyncHttpClient();

        if (file.exists() && file.length() > 0) {
            RequestParams params = new RequestParams();
            try {
                params.put("file", file);
            } catch (Exception e) {
                e.printStackTrace();
            }

            client.post(url, params, new AsyncHttpResponseHandler() {

                @Override
                public void onSuccess(int statusCode, Header[] headers,byte[] responseBody) {
                    QLog.d(this, "上传成功");
                }

                @Override
                public void onFailure(int statusCode, Header[] headers,byte[] responseBody, Throwable error) {
                    QLog.d(this, "上传失败: code; " + statusCode);
                }
            });
        } else {
            App.showToast("文件不存在");
        }

        /*AsyncHttpClient client = new AsyncHttpClient();
        client.post("http://192.168.1.9:8080/logServer/upload.do", new AsyncHttpResponseHandler() {

            @Override
            public void onStart() {

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                // called when response HTTP status is "200 OK"\
                if (statusCode == 200) {
                    QLog.d(this, "链接成功");
                    File myFile = new File(filePath);
                    RequestParams params = new RequestParams();
                    try {
                        params.put("upload_load_file_name", myFile);
                    } catch (FileNotFoundException e) {
                    }
                } else {
                    QLog.d(this, "链接失败" + statusCode);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
                // called when response HTTP status is "4XX" (eg. 401, 403, 404)
            }

            @Override
            public void onRetry(int retryNo) {
                // called when request is retried
            }
        });*/
        UiObserverManager.getInstance().dispatchEvent(CMD_UPLOAD_LOG, false, filePath, null);
    }

}
